/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import Lib.Transport;
import java.io.IOException;
import java.net.UnknownHostException;

/**
 *
 * @author Mickael
 */
public class ModeleDistant implements InterfaceModele {

    private ProtocolClientInterface pCi;
    int port;
    String host;

    public ModeleDistant() {
    }

    
    
    
    @Override
    public double demandeAnnuit(double montant, double taux, int nbannee) throws ErreurService {
        try {
            creerCoucheTransport();
        } catch (UnknownHostException ex) {
            throw new ErreurService();
        } catch (IOException ex) {
            throw new ErreurService();
        }
        DonneeResultat res = null;
        try {
            pCi.callDemandeAnnuit(montant, taux, nbannee);
            res = pCi.returnDdeAnnuite();
        } catch (ErreurReceptionReponse ex) {
            throw new ErreurService();
        } catch (ErreurFermetureClientException ex) {
            throw new ErreurService();
        } catch (ErreurMontantAnnuiteException ex) {
            throw new ErreurService();
        } catch (ErreurDemandeException ex) {
            throw new ErreurService();
        } catch (ErreurMontantPretException ex) {
            throw new ErreurService();
        } catch (ErreurNombreAnneeException ex) {
            throw new ErreurService();
        } catch (ErreurSurTauxException ex) {
            throw new ErreurService();
        }
        return res.getRes();
    }

    @Override
    public double demandeNbAnnee(double montant, double taux, double annuite) throws ErreurService {
        try {
            creerCoucheTransport();
        } catch (UnknownHostException ex) {
            throw new ErreurService();
        } catch (IOException ex) {
            throw new ErreurService();
        }
        DonneeResultat res = null;
        try {
            pCi.callDemandeNbreAnnee(montant, taux, annuite);
            res = pCi.returnDdeAnnuite();
        } catch (ErreurReceptionReponse ex) {
            throw new ErreurService();
        } catch (ErreurFermetureClientException ex) {
            throw new ErreurService();
        } catch (ErreurNombreAnneeException ex) {
            throw new ErreurService();
        } catch (ErreurDemandeException ex) {
            throw new ErreurService();
        } catch (ErreurMontantPretException ex) {
            throw new ErreurService();
        } catch (ErreurSurTauxException ex) {
            throw new ErreurService();
        } catch (ErreurMontantAnnuiteException ex) {
            throw new ErreurService();
        }
        return res.getRes();
    }

    @Override
    public void finaliser() throws ErreurService {
        try {
            pCi.fermer();
        } catch (ErreurFermetureClientException ex) {
            throw new ErreurService();
        }
    }

    public void setHost(String host) {
        this.host = host;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    private void creerCoucheTransport() throws UnknownHostException, IOException {
        Transport t = new Transport();
        t.setHostPort(host, port);
        pCi = new ProtocolClient(t);
    }
}
